package util;

import java.io.InputStream;
import java.sql.Connection;
import java.sql.DriverManager;
import java.util.Properties;

public class DBUtil {
	private static String driver;
	private static String url;
	private static String username;
	private static String password;
	static {
		try {
			InputStream in = DBUtil.class.getClassLoader()
				.getResourceAsStream("db.properties");
			Properties cfg = new Properties();
			cfg.load(in);
			in.close();
			driver = cfg.getProperty("driver");
			url = cfg.getProperty("url");
			username=cfg.getProperty("username");
			password=cfg.getProperty("password");
		}catch(Exception e) {
			e.printStackTrace();
			throw new RuntimeException(e);
		}
		
	}
	
	public static Connection getConnection() 
		throws Exception{
		Class.forName(driver);
		Connection conn = DriverManager
			.getConnection(url,username,password);
		return conn;
	}
	
	public static void close(Connection conn) {
		try {
			if(conn!=null) {
				conn.close();
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
	
}






